package cn.wwcyc.controller;

import cn.wwcyc.domain.*;
import cn.wwcyc.mapper.MenuMapper;
import cn.wwcyc.mapper.RolemenuMapper;
import cn.wwcyc.mapper.TjMenugroupMapper;
import cn.wwcyc.mapper.TjRolegroupMapper;
import cn.wwcyc.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/menugroup")
public class MenuGroupController {
    @Autowired
    TjMenugroupMapper tjMenugroupMapper;
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    TjRolegroupMapper rolegroupMapper;
    @Autowired
    RolemenuMapper tjRoleMenuMapper;
    @Autowired
    UserService userService;

    @GetMapping
    public R<List<TjMenugroup>> getList() {
        QueryWrapper<TjMenugroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("xh");
        List<TjMenugroup> list = tjMenugroupMapper.selectList(queryWrapper);
        return R.success(list);
    }
    //以树形获取所有的菜单组及菜单，同时获取某个角色拥有权限的菜单id列表，对菜单组通过hasRights属性来标识是否拥有权限
    //以上所有信息都在 TjMenugroupWithRights中
    @GetMapping("getAllWithZRights")
    public R<List<TjMenugroupWithRights>> getAllWithZRights(@RequestParam Long roleid,
                                                            HttpServletRequest request) {
        User user = userService.getCurrentUser(request);
        List<TjMenugroupWithRights> listWithRights = new ArrayList<>();
        QueryWrapper<TjMenugroup> mgQueryWrapper = new QueryWrapper<>();
        mgQueryWrapper.orderByAsc("xh");
        List<TjMenugroup> listAll = tjMenugroupMapper.selectList(mgQueryWrapper);
        listAll.forEach(item -> {
            item.setMenuTreeList(menuMapper.selectMenuTreeListWithGroupByGroupid(item.getId()));
//            if(user.getSuperadmin() != null && user.getSuperadmin() == true)//超级用户
//                item.setMenuTreeList(menuMapper.selectMenuTreeListWithGroupByGroupid(item.getId()));
//            else//普通用户
//                item.setMenuTreeList(menuMapper.selectMenuTreeListWithGroupByGroupidOrd(item.getId()));
        });
        listAll.forEach(item -> {
            TjMenugroupWithRights tjMenugroupWithRights = new TjMenugroupWithRights();
            tjMenugroupWithRights.setId(item.getId());
            tjMenugroupWithRights.setName(item.getName());
            tjMenugroupWithRights.setRootmenuids(item.getRootmenuids());
            tjMenugroupWithRights.setXh(item.getXh());
            tjMenugroupWithRights.setRoutepath(item.getRoutepath());
            tjMenugroupWithRights.setMenuTreeList(item.getMenuTreeList());
            listWithRights.add(tjMenugroupWithRights);
        });
        QueryWrapper<TjRolegroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roleid", roleid);
        List<TjRolegroup> listRG = rolegroupMapper.selectList(queryWrapper);
        List<Long> listRGId = new ArrayList<>();
        for(int i=0; i<listRG.size(); i++){
            listRGId.add(listRG.get(i).getGroupid());
        }
        for(int i=0; i<listWithRights.size(); i++){
            if(listRGId.contains(listWithRights.get(i).getId())){
                listWithRights.get(i).setHasRights(true);
            }
        }
        listWithRights.forEach(item -> {
            item.setMenuRightIds(tjRoleMenuMapper.selectMenuIdByRoleIdandMenuGroupId(roleid, item.getId()));
        });
        return R.success(listWithRights);
    }

    @GetMapping("getAll")
    public R<List<TjMenugroup>> getAll() {
        List<TjMenugroup> list = tjMenugroupMapper.selectList(null);
        list.forEach(item -> {
            item.setMenuTreeList(menuMapper.selectMenuTreeListWithGroupByGroupid(item.getId()));
        });

        return R.success(list);
    }

    @GetMapping("getByRole")
    public R<List<TjMenugroup>> getByRole(@RequestParam Integer roleId) {
//        List<TjMenugroup> list = tjMenugroupMapper.getGroupWithMenuTreeByRole(roleId);
//        return R.success(list);
        return null;
    }

    @PostMapping("/save")
    public R save(@RequestBody TjMenugroup tjMenugroup) {
        tjMenugroupMapper.insert(tjMenugroup);
        return R.success(null);
    }

    @PostMapping("/edit")
    public R edit(@RequestBody TjMenugroup tjMenugroup) {
        tjMenugroupMapper.updateById(tjMenugroup);
        return R.success(null);
    }
    @GetMapping("/delete")
    public R delete(@RequestParam Long id) {
        tjMenugroupMapper.deleteById(id);
        return R.success(null);
    }
}
